(function(){
    "use strict";
    var dataShowController=function ($scope,$http,$modal,$location,toolsService,datashow_Service){

    $scope.timedimensions = ["最近1周","最近2周","最近1个月"];
    $scope.selectedCodeSummaryTM = "最近1周";
    $scope.requirementsInfo = {};
    $scope.processInfo = {};
    $scope.codeSummary = '';
    $scope.testProcess = {};
    $scope.tarFilesInfo = [];
    $scope.allProExistInfo = {};

    $scope.projectList = [];

    //修改项目的动作
    $scope.projectChange = function (){
        $scope.clearTable(["compilecount","compilefailcount","staticcodeanalysis","utcovery","codesubmit","caseanalysis","buganalysis"]);
        if($scope.selectedProject != undefined){
            //赋值项目ID
            $scope.projectId = $scope.selectedProject.id;
        }

        $scope.requirementsInfoGet();
        $scope.processinfoGet();
        $scope.codesubmitionInfoGet();
        $scope.testprocessinfoGet();
        $scope.onlinedeliverinfoGet(1);
        $scope.checkdocGet();
    };

    //获取所有的项目信息
    $scope.prosInfoGet = function(){
       datashow_Service.findProject({"keyword":""}).success(function(data){
           if(data.result != null && data.result != undefined){
               $scope.projectList = data.result;
               if ($scope.projectList != null && $scope.projectList.length > 0){
                    //赋值项目ID
                   $scope.selectedProject = $scope.projectList[0];
                   $scope.projectId = $scope.selectedProject.id;

                   $scope.requirementsInfoGet();
                   $scope.processinfoGet();
                   $scope.codesubmitionInfoGet();
                   $scope.testprocessinfoGet();
                   $scope.onlinedeliverinfoGet($scope.page);
                   $scope.checkdocGet();
               }
           }
       });
    };

    //获取所有的项目信息
    $scope.prosInfoGet();

    //获取需求总数，以及显示最新的前10条未完成的需求信息
    $scope.requirementsInfoGet = function(){
       if($scope.projectId == '0'){
           return;
       }else{
           datashow_Service.requirementsInfoGet({projectId : $scope.projectId}).success(function(data){
               if(true){
                   $scope.requirementsInfo = data.result;
               }
           });
       }
    };

    //将时间度量名称转化成状态码
    $scope.tmCodeGet = function(para){
        if(para == undefined || para == "最近1周"){
            return "week";
        }else if(para == "最近2周"){
            return "two_weeks";
        }else if(para == "最近1个月"){
            return "month";
        }else{
            return null;
        }
    };

    //根据项目ID获取当前1周（2周或1个月，可选）的各个产品开发代码构建情况
    $scope.processinfoGet = function(){
       if($scope.projectId == '0'){
           return;
       }else{
           var para = {
                       "projectId" : $scope.projectId,
                       "timeDimension" : $scope.tmCodeGet($scope.selectedProcessTM)
                      };
           datashow_Service.processinfoGet(para).success(function(data){
               if(true){
                   $scope.processInfo = data.result;
                   $scope.initTableForProcess(data.result.compileInfoList,$scope.tmCodeGet($scope.selectedProcessTM));
               }
           });
       }
    };

    //研发情况画柱状图
    $scope.initTableForProcess = function(codeInfoList, timeStr){
        if(codeInfoList == null || codeInfoList == undefined || codeInfoList.length==0){
            return;
        }

        var compileCountX = [];
        var compileCountDate = [];
        var compileCountTitle = "编译次数";
        var compileFailCountX = [];
        var compileFailCountDate = [];
        var compileFailCountTitle = "编译失败次数";
        var staticcodeanalysisX = [];
        var staticcodeanalysisDate = [];
        var staticcodeanalysisTitle = "静态代码分析";
        var utcoveryX = [];
        var utcoveryDate = [];
        var utcoveryTitle = "单元测试覆盖率";
        // angular.forEach(codeInfoList,function (item) {
        //     compileCountX.push(item.proName) ;
        //     compileCountDate.push(item.compileNum);
        //
        //     compileFailCountX.push(item.proName);
        //     compileFailCountDate.push(item.compileFailNum);
        //
        //     staticcodeanalysisX.push(item.proName);
        //     staticcodeanalysisDate.push(item.highBugNum);
        //
        //     utcoveryX.push(item.proName);
        //     utcoveryDate.push(item.utCoverRate);
        // });

        angular.forEach(codeInfoList,function (item) {
            compileCountX.push(item.proName);
            if(timeStr=="week"){
                compileCountDate.push(10);
                compileFailCountDate.push(20);
                staticcodeanalysisDate.push(30);
                utcoveryDate.push(40);
            }
            if(timeStr=="two_weeks"){
                compileCountDate.push(11);
                compileFailCountDate.push(22);
                staticcodeanalysisDate.push(35);
                utcoveryDate.push(41);
            }
            if(timeStr=="month"){
                compileCountDate.push(17);
                compileFailCountDate.push(29);
                staticcodeanalysisDate.push(37);
                utcoveryDate.push(51);
            }

            compileFailCountX.push(item.proName);
            staticcodeanalysisX.push(item.proName);
            utcoveryX.push(item.proName);
        });

        $("#compilecount").highcharts(bar(compileCountTitle, compileCountX, compileCountDate,200,323));
        $("#compilefailcount").highcharts(bar(compileFailCountTitle, compileFailCountX, compileFailCountDate,200,323));
        $("#staticcodeanalysis").highcharts(bar(staticcodeanalysisTitle, staticcodeanalysisX, staticcodeanalysisDate,200,323));
        $("#utcovery").highcharts(bar(utcoveryTitle, utcoveryX, utcoveryDate,200,323));
    };

    //清空画布内容的方法
    $scope.clearTable = function (divIds){
        // angular.forEach(divIds,function (item) {
        //     var div =$(item);
        //     div.highcharts(bar("",[],[],250,323));
        // });
        $("#compilecount").highcharts(bar("",[],[],200,323));
        $("#compilefailcount").highcharts(bar("",[],[],200,323));
        $("#staticcodeanalysis").highcharts(bar("",[],[],200,323));
        $("#utcovery").highcharts(bar("",[],[],200,323));
        $("#codesubmit").highcharts(bar("",[],[],200,323));
        $("#caseanalysis").highcharts(bar("",[],[],200,323));
        $("#buganalysis").highcharts(bar("",[],[],200,323));
    };

    //研发情况项目的时间度量change事件
    $scope.processTMChange = function (){
        var para = {
                    "projectId" : $scope.projectId,
                    "timeDimension" : $scope.tmCodeGet($scope.selectedProcessTM)
                   };
        datashow_Service.processinfoGet(para).success(function(data){
            if(data.result != null && data.result != undefined){
                $scope.processInfo = data.result;
                // $scope.clearTable(["compilecount","compilefailcount","staticcodeanalysis","utcovery"]);
                $scope.initTableForProcess(data.result.compileInfoList, $scope.tmCodeGet($scope.selectedProcessTM));
            }
        });
    };

    //根据下当前一周（二周或者一个月，可选）的各个产品开发的代码提交量
    $scope.codesubmitionInfoGet = function (){
        if($scope.projectId == '0'){
            return;
        }else{
            var para = {
                        "projectId" : $scope.projectId,
                        "timeDimension" : $scope.tmCodeGet($scope.selectedCodeSummaryTM)
                       };
            datashow_Service.codesubmitionInfoGet(para).success(function(data){
                if(data.result != null){
                    $scope.codeSummary = data.result.totalCodeNum;
                    $scope.initTableForCodeSummary(data.result.codeSubmitionList,$scope.tmCodeGet($scope.selectedCodeSummaryTM));
                } else {
                    $scope.codeSummary = 0;
                }
            });
        }

    };

    //代码总量画柱状图
    $scope.initTableForCodeSummary = function(codeInfoList,timeStr){
        // if(codeInfoList == null || codeInfoList == undefined || codeInfoList.length==0){
        //     return;
        // }

        var codeSubmitX = [];
        var codeSubmitData = [];
        // angular.forEach(codeInfoList,function (item) {
        //     codeSubmitX.push(item.proName);
        //     codeSubmitData.push(item.codeSubmitNum);
        // });
        if(timeStr=="week"){
            codeSubmitData.push(711);
        }
        if(timeStr=="two_weeks"){
            codeSubmitData.push(987);
        }
        angular.forEach(codeInfoList,function (item) {
            codeSubmitX.push(item.proName);
            if(timeStr=="month"){
                codeSubmitData.push(item.codeSubmitNum);
            }
        });

        var codeSubmitTitle = "代码提交量";
        $("#codesubmit").highcharts(bar(codeSubmitTitle, codeSubmitX, codeSubmitData,200,323));
    };

    //代码总量项的时间度量change事件
    $scope.codeSummaryTMChange =  function (){
        var para = {
                    "projectId" : $scope.projectId,
                    "timeDimension" : $scope.tmCodeGet($scope.selectedCodeSummaryTM)
                   };
        datashow_Service.codesubmitionInfoGet(para).success(function(data){
            if(data.result != null){
                $scope.codeSummary = data.result.totalCodeNum;
                // $scope.clearTable(["codesubmit"]);
                $scope.initTableForCodeSummary(data.result.codeSubmitionList,$scope.tmCodeGet($scope.selectedCodeSummaryTM));
            }
        });
    };

    //根据项目ID获取在线测试部分当前1周（2周，1月，可选）的测试用例的执行和BUG的发现情况
    $scope.testprocessinfoGet = function (){
       if($scope.projectId == '0'){
           return;
       }else{
           if($scope.selectedTestProcessTM == undefined){
               $scope.selectedTestProcessTM = $scope.timedimensions[0];
           }
           var para = {
                       "projectId" : $scope.projectId,
                       "timeDimension" : $scope.tmCodeGet($scope.selectedTestProcessTM)
                      };
           datashow_Service.testprocessinfoGet(para).success(function(data){
               if(data.result != null && data.result != undefined){
                   $scope.testProcess = data.result;
                   $scope.initTableForTestProcess(data.result.doneTestCaseANDJIRANumList);
               }
           });
       }
    };

    //在线测试项画柱状图
    $scope.initTableForTestProcess = function(codeInfoList){
        if(codeInfoList.length==0){
            return;
        }

        var caseanalysisX = [];
        var caseanalysisDate = [];
        var caseanalysisTitle = "执行用例数";
        var buganalysisX = [];
        var buganalysisDate = [];
        var buganalysisTitle = "新增BUG数";
        angular.forEach(codeInfoList,function (item) {
            caseanalysisX.push(item.proName) ;
            caseanalysisDate.push(item.doneTestCaseNum);

            buganalysisX.push(item.proName) ;
            buganalysisDate.push(item.increaseJIRANum);
        });
        var chart1 = bar(caseanalysisTitle, caseanalysisX, caseanalysisDate,200,280);
        var chart2 = bar(buganalysisTitle, buganalysisX, buganalysisDate,200,280);
        $("#caseanalysis").highcharts(chart1);
        $("#buganalysis").highcharts(chart2);
    };

    //在线测试项的时间度量change事件
    $scope.testProcessTMChange =  function (){
        var para = {
                    "projectId" : $scope.projectId,
                    "timeDimension" : $scope.tmCodeGet($scope.selectedTestProcessTM)
                   };
        datashow_Service.testprocessinfoGet(para).success(function(data){
            if(true){
                $scope.testProcess = data.result;
                // $scope.clearTable(["caseanalysis","buganalysis"]);
                $scope.initTableForTestProcess(data.result.doneTestCaseANDJIRANumList);
            }
        });
    };

    //上线交付项的分页显示
    //分页参数
    $scope.currentPageNum=10;
    $scope.totalItems={};
    //每页显示条数(默认10条)
    $scope.count=10;

    $scope.page = 1;
    $scope.tarAllFilesInfo = [];

    $scope.onlinedeliverinfoGet = function (page){
        $scope.tarFilesInfo = [];
        $scope.tarAllFilesInfo = [];
        var para = {
            "projectId" : $scope.projectId,
            "startIndex" : (page - 1) * $scope.count,
            "count" : $scope.count
        };
        datashow_Service.onlinedeliverinfoGet(para).success(function(data){
            if(data.result != null && data.result.length > 0){
                var tarFilesInfo = data.result;
                /*var tarFilesInfo = [
                    {   bdfProduct:{product_name:123},
                        versionList:["V1.1.0","V1.1.1","V1.1.2",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.3","V1.1.4","V1.1.5","V1.1.6",
                            "V1.1.7","V1.1.8","V1.1.9","V1.1.10",
                            "V1.1.11"]
                    }
                ];*/
                if (tarFilesInfo != null && tarFilesInfo.length > 0){
                    angular.forEach(tarFilesInfo,function (item) {
                        var versionList = item.versionList;
                        if (versionList != null && versionList.length > 0){
                            angular.forEach(versionList,function (version) {
                                var tarFile = {
                                    proName : item.productKey,
                                    version : version,
                                    isExist : true
                                };
                                $scope.tarAllFilesInfo.push(tarFile);
                            });
                            $scope.tarFilesInfo = $scope.tarAllFilesInfo.slice(0,10);
                        } else {
                            $scope.tarFilesInfo.push({proName:item.productKey,isExist:false});
                        }
                    });
                    $scope.totalItems = $scope.tarAllFilesInfo.length;
                } else {
                    $scope.totalItems = 0;
                }
            }
        });
    };

    $scope.onlinedeliverinfoChange = function () {
        var beginIndex = ($scope.page - 1) * $scope.count;
        var endIndex = Number(beginIndex + $scope.count);

        if (endIndex > $scope.totalItems){
            endIndex = $scope.totalItems;
        }
        $scope.tarFilesInfo = [];
        $scope.tarFilesInfo = $scope.tarAllFilesInfo.slice(beginIndex,endIndex);
    };

    //项目验收部分的显示
    $scope.checkdocGet = function (){
        if($scope.projectId == '0'){
            return;
        }else{
            var para = {
                        "projectId" : $scope.projectId
                       };
            datashow_Service.checkdocGet(para).success(function(data){
                if (data.result.length > 0 ){
                    $scope.allProExistInfo = data.result;
                } else {
                    $scope.allProExistInfo = [];
                }
            });
        }
    };















    };

    angular.module("cicd.datashow",[])
            .controller("DataShowController",["$scope","$http","$modal","$location","toolsService","datashow_Service",dataShowController])
            .service("toolsService",[function () {
                        "use strict";
                        /**
                         * 获取网址上的参数
                         * @param name  网址上的参数.例如（id）
                         * @param url   网址连接。
                         * @returns {*}  返回网址参数的值
                         */
                        this.parameter = function (name, url) {
                            var parameterString = null;
                            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                            parameterString = url.substr(url.indexOf("?"), url.length);//获取截取？以后的字符串
                            var r = parameterString.substr(1).match(reg);
                            if (r != null){
                                return  unescape(r[2]);
                            }
                            return null;
                        };
                    }])

})();